package com.share.simple.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.simple.mapper.RoleMapper;
import com.share.simple.model.entity.Role;
import com.share.simple.model.entity.UserRole;
import com.share.simple.model.vo.RoleVo;
import com.share.simple.service.RoleService;
import com.share.simple.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:caifan
 * @since 2023/7/10
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private UserRoleService userRoleService;

    @Override
    public List<RoleVo> listRoleByUserId(Long userId) {
        List<UserRole> userRoleList = userRoleService.list(Wrappers.<UserRole>lambdaQuery().eq(UserRole::getUserId, userId));
        if (CollectionUtil.isEmpty(userRoleList)) {
            return Collections.emptyList();
        }
        List<Role> roles = this.listByIds(userRoleList.stream().map(UserRole::getRoleId).distinct().collect(Collectors.toList()));
        return BeanUtil.copyToList(roles, RoleVo.class);
    }
}
